-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: slice_indexer correction + examples #17829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC: slice_indexer correction + examples #17829
Conversation
ccc34b2
to
bb8050a
Compare
Codecov Report
@@ Coverage Diff @@
## master #17829 +/- ##
=========================================
Coverage ? 91.22%
=========================================
Files ? 163
Lines ? 49994
Branches ? 0
=========================================
Hits ? 45606
Misses ? 4388
Partials ? 0
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #17829 +/- ##
==========================================
- Coverage 91.26% 91.22% -0.05%
==========================================
Files 163 163
Lines 50099 50099
==========================================
- Hits 45724 45703 -21
- Misses 4375 4396 +21
Continue to review full report at Codecov.
|
pandas/core/indexes/base.py
Outdated
@@ -3412,7 +3412,7 @@ def _get_string_slice(self, key, use_lhs=True, use_rhs=True): | |||
|
|||
def slice_indexer(self, start=None, end=None, step=None, kind=None): | |||
""" | |||
For an ordered Index, compute the slice indexer for input labels and | |||
For an ordered index, compute the slice indexer for input labels and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not fully sure it has to be ordered? When there are no duplicates, I think it doesn't need to be sorted:
In [9]: pd.Index([1, 3, 4, 2, 5]).slice_indexer(1, 2)
Out[9]: slice(0, 4, None)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can add a "Raises" section to specify when a KeyError is raised:
In [10]: pd.Index([1, 3, 4, 2, 5, 2]).slice_indexer(1, 2)
...
KeyError: 'Cannot get right slice bound for non-unique label: 2'
In [11]: pd.Index([1, 3, 4, 2, 5, 2]).slice_indexer(1, 4)
Out[11]: slice(0, 3, None)
pandas/core/indexes/base.py
Outdated
@@ -3426,11 +3426,23 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None): | |||
|
|||
Returns | |||
------- | |||
indexer : ndarray or slice | |||
indexer : slice | |||
|
|||
Notes | |||
----- | |||
This function assumes that the data is sorted, so use at your own peril |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although there is this note .. Not fully sure what it means ..
0652130
to
e78f3cd
Compare
Ok, I've changed according to the suggestions. I think the warning is ok, because I'm a bit surprised that it's even possible to slice a non-ordered unique index; maybe you'd want that some times, but I bet 9 out of 10 times, someone who slices a index expects it to be ordered. |
@topper-123 Thanks! |
The stated return value of
Index.slice_indexer
in the doc string is wrong, the method can only return slices. Also added some examples.In addition, some very minor corrections on the user guide text on
IntervalIndex
(moved the versionadded to the chapter top + a spelling correction). EDIT: These are not related to the.slice_indexer
changes, but are attached to this PR as the changes are very small.